﻿using Microsoft.AspNetCore.Mvc.Routing;

namespace Web
{
    /// <summary>
    /// 自定义路由
    /// </summary>
    public class DemoRouteValueTransformer : DynamicRouteValueTransformer
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="httpContext"></param>
        /// <param name="values"></param>
        /// <returns></returns>
        public override ValueTask<RouteValueDictionary> TransformAsync(HttpContext httpContext, RouteValueDictionary values)
        {
            if (httpContext.Request.Headers.TryGetValue("X-Forwarded-Host", out var forwardedHost)
                && forwardedHost.ToString().Equals("q.cnblogs.com", StringComparison.OrdinalIgnoreCase))
            {
                values = values ?? new RouteValueDictionary();
                values["controller"] = "Home";
                values["action"] = "Demo";
            }

            return ValueTask.FromResult(values);
        }
    }
}
